1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.tuple;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  
29  import org.junit.Test;
30  
31  /**
32   * Test the Triple class.
33   * @version $Id$
34   */
35  public class ImmutableTripleTest {
36  
37      @Test
38      public void testBasic() throws Exception {
39          final ImmutableTriple<Integer, String, Boolean> triple = new ImmutableTriple<Integer, String, Boolean>(0, "foo", Boolean.TRUE);
40          assertEquals(0, triple.left.intValue());
41          assertEquals(0, triple.getLeft().intValue());
42          assertEquals("foo", triple.middle);
43          assertEquals("foo", triple.getMiddle());
44          assertEquals(Boolean.TRUE, triple.right);
45          assertEquals(Boolean.TRUE, triple.getRight());
46          final ImmutableTriple<Object, String, Integer> triple2 = new ImmutableTriple<Object, String, Integer>(null, "bar", 42);
47          assertNull(triple2.left);
48          assertNull(triple2.getLeft());
49          assertEquals("bar", triple2.middle);
50          assertEquals("bar", triple2.getMiddle());
51          assertEquals(new Integer(42), triple2.right);
52          assertEquals(new Integer(42), triple2.getRight());
53      }
54  
55      @Test
56      public void testTripleOf() throws Exception {
57          final ImmutableTriple<Integer, String, Boolean> triple = ImmutableTriple.of(0, "foo", Boolean.FALSE);
58          assertEquals(0, triple.left.intValue());
59          assertEquals(0, triple.getLeft().intValue());
60          assertEquals("foo", triple.middle);
61          assertEquals("foo", triple.getMiddle());
62          assertEquals(Boolean.FALSE, triple.right);
63          assertEquals(Boolean.FALSE, triple.getRight());
64          final ImmutableTriple<Object, String, Boolean> triple2 = ImmutableTriple.of(null, "bar", Boolean.TRUE);
65          assertNull(triple2.left);
66          assertNull(triple2.getLeft());
67          assertEquals("bar", triple2.middle);
68          assertEquals("bar", triple2.getMiddle());
69          assertEquals(Boolean.TRUE, triple2.right);
70          assertEquals(Boolean.TRUE, triple2.getRight());
71      }
72  
73      @Test
74      public void testEquals() throws Exception {
75          assertEquals(ImmutableTriple.of(null, "foo", 42), ImmutableTriple.of(null, "foo", 42));
76          assertFalse(ImmutableTriple.of("foo", 0, Boolean.TRUE).equals(ImmutableTriple.of("foo", null, null)));
77          assertFalse(ImmutableTriple.of("foo", "bar", "baz").equals(ImmutableTriple.of("xyz", "bar", "blo")));
78  
79          final ImmutableTriple<String, String, String> p = ImmutableTriple.of("foo", "bar", "baz");
80          assertTrue(p.equals(p));
81          assertFalse(p.equals(new Object()));
82      }
83  
84      @Test
85      public void testHashCode() throws Exception {
86          assertEquals(ImmutableTriple.of(null, "foo", Boolean.TRUE).hashCode(), ImmutableTriple.of(null, "foo", Boolean.TRUE).hashCode());
87      }
88  
89      @Test
90      public void testToString() throws Exception {
91          assertEquals("(null,null,null)", ImmutableTriple.of(null, null, null).toString());
92          assertEquals("(null,two,null)", ImmutableTriple.of(null, "two", null).toString());
93          assertEquals("(one,null,null)", ImmutableTriple.of("one", null, null).toString());
94          assertEquals("(one,two,null)", ImmutableTriple.of("one", "two", null).toString());
95          assertEquals("(null,two,three)", ImmutableTriple.of(null, "two", "three").toString());
96          assertEquals("(one,null,three)", ImmutableTriple.of("one", null, "three").toString());
97          assertEquals("(one,two,three)", MutableTriple.of("one", "two", "three").toString());
98      }
99  
100     @Test
101     @SuppressWarnings("unchecked")
102     public void testSerialization() throws Exception {
103         final ImmutableTriple<Integer, String, Boolean> origTriple = ImmutableTriple.of(0, "foo", Boolean.TRUE);
104         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
105         final ObjectOutputStream out = new ObjectOutputStream(baos);
106         out.writeObject(origTriple);
107         final ImmutableTriple<Integer, String, Boolean> deserializedTriple = (ImmutableTriple<Integer, String, Boolean>) new ObjectInputStream(
108                 new ByteArrayInputStream(baos.toByteArray())).readObject();
109         assertEquals(origTriple, deserializedTriple);
110         assertEquals(origTriple.hashCode(), deserializedTriple.hashCode());
111     }
112 }
113